/**
 * Copyright (C) 2012  The CFuture Android SDK Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.android.toolkit.database;

import android.content.Context;

import com.lurencun.android.toolkit.HasContext;

/**
 * 一个Key需要被校验
 * 如果这个Key存在于数据库中，
 * 则这个Key没有被锁定
 * 
 * @author cfuture.chenyoca [桥下一粒砂] (chenyoca@163.com)
 * @date 2012-3-1
 */
public class KeyLocker extends HasContext {
	
	private final static String SHARE_DB_NAME = "KEY_LOCKER_SHARE";
	private final static String ALL_KEY_NAME = "UNLOCK_ALL_KEY";
	private ShareDBHelper mShare;
	
	/**
	 * @param context
	 */
	public KeyLocker(Context context) {
		super(context);
		mShare = new ShareDBHelper(mContext, SHARE_DB_NAME);
	}
	
	/**
	 * 解锁这个KEY
	 * @param keyName
	 */
	public void unlock(String keyName){
		mShare.put(keyName, keyName);
	}
	
	/**
	 * 判断这个Key是否被锁定
	 * @param keyName
	 * @return 这个Key已经被锁定则返回true，否则返回false
	 */
	public boolean isLocked(String keyName){
		if(isAllUnlock()){
			return false;
		}else{
			return !mShare.getString(keyName).equals(keyName);
		}
	}
	
	/**
	 * 解锁全部KEY
	 */
	public void unlockAll(){
		mShare.put(ALL_KEY_NAME, true);
	}
	
	/**
	 * 清除全部解锁标识
	 */
	public void cleanAll(){
		mShare.put(ALL_KEY_NAME, false);
	}
	
	/**
	 * 是否全部打开
	 * @return
	 */
	public boolean isAllUnlock(){
		return mShare.getBoolean(ALL_KEY_NAME);
	}

}
